package com.canbot.u05.adapter.sales;

import android.app.ActionBar;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.adapter.MerchandiseAdapter;
import com.canbot.u05.bean.Merchandise;
import com.canbot.u05.bean.sales.SellInfo;
import com.canbot.u05.utils.Logger;

import java.util.Collections;
import java.util.List;


/**
 * @ClassName: 售卖宣传模式
 */
public class SellModePropagandaAdapter extends RecyclerView.Adapter<SellModePropagandaAdapter.ViewHolder> implements OnClickListener {
        private static final String TAG = "SellModePropagandaAdapter";
        private List<SellInfo> dList;

        private Context context;

        private LayoutInflater mInflater;

        private PopupWindow popupWindow;

        private int oldId = -1;

        public int index;

        private RecyclerView listView;

        public SellModePropagandaAdapter(Context context, List<SellInfo> dList,RecyclerView listView) {
                super();
                this.context = context;
                mInflater = LayoutInflater.from(context);
                this.dList = dList;
                this.listView = listView;
                this.dList = Collections.synchronizedList(dList);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View inflate = mInflater.inflate(R.layout.sellmode_propaganda_item, parent, false);
                return new SellModePropagandaAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
                holder.position = position;
                holder.title.setText(dList.get(position).getTitle());
                holder.relativeLayout.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                                // 点击事件
                                if (mItemClickListener != null){
                                        mItemClickListener.onItemClick(position);
                                }
                        }
                });
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public int getItemCount() {
                return dList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

                RelativeLayout relativeLayout;

                public ImageView imageView;

                public TextView title;

                public int position;

                public ViewHolder(View itemView) {
                        super(itemView);
                        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.rel_1);
                        title = (TextView) itemView.findViewById(R.id.text1);
                        imageView = (ImageView)  itemView.findViewById(R.id.imageView);
                        imageView.setOnClickListener(this);

                }

                @Override
                public void onClick(View v) {
                        switch (v.getId()){

                                case R.id.imageView:
                                        showPopupWindow(imageView,position);
                                        break;
                        }
                }
        }

        /**
         * 弹框的显示
         */
        public void showPopupWindow(View view, int position) {

                // 一个自定义的布局，作为显示的内容
                View contentView = LayoutInflater.from(context).inflate(R.layout.sellmode_item_menu, null);
                Log.e("contentView", contentView + "");


                // 设置按钮的点击事件
                TextView textView = (TextView) contentView.findViewById(R.id.sell_title);
                textView.setText(dList.get(position).getTitle());


                Button button1 = (Button) contentView.findViewById(R.id.btn_1);
                Button buttonname = (Button) contentView.findViewById(R.id.btn_name);
                Button buttondelete = (Button) contentView.findViewById(R.id.btn_delet);

                button1.setOnClickListener(this);
                buttonname.setOnClickListener(this);
                buttondelete.setOnClickListener(this);

                popupWindow = new PopupWindow(contentView,
                        (int)context.getResources().getDimension(R.dimen.x300), ActionBar.LayoutParams.WRAP_CONTENT, true);


                popupWindow.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Log.i("mengdd", "onTouch : ");
                                return false;
                                // 这里如果返回true的话，touch事件将被拦截
                                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

                        }
                });

                // 设置好参数之后再show
                //popupWindow.showAsDropDown(view);
                int[] location = new int[2];
                view.getLocationOnScreen(location);

                int visibleLastPosi = -1;
                RecyclerView.LayoutManager layoutManager = listView.getLayoutManager();
                //判断是当前layoutManager是否为LinearLayoutManager
                // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
                if (layoutManager instanceof LinearLayoutManager) {
                        LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                        //获取最后一个可见view的位置
                        visibleLastPosi = linearManager.findLastVisibleItemPosition();
                }

                Logger.e(TAG,"visibleLastPosi = " + visibleLastPosi);
                Logger.e(TAG,"position = " + position);


                if (position <= visibleLastPosi-4){
                        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                                R.drawable.menu_background));
                        popupWindow.showAsDropDown(view,-(int)context.getResources().getDimension(R.dimen.x279),-(int)context.getResources().getDimension(R.dimen.x40));

                }else {
                        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                                R.drawable.menu_background2));
                        popupWindow.showAsDropDown(view,-(int)context.getResources().getDimension(R.dimen.x279),-(int)context.getResources().getDimension(R.dimen.x260));
                }


//                popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0] - 220 - 40 - 20 - 10, location[1] - 13);
                if (listner != null) {
                        listner.showPopuwindow(position);
                }

        }

        public interface AdapterOnclickListner {

                void onclick(int id);

                void showPopuwindow(int position);

                void doTask();
        }

        public interface OnItemClickListener{
                void onItemClick(int position);
        }

        SellModePropagandaAdapter.AdapterOnclickListner listner;

        private OnItemClickListener mItemClickListener;

        public void setListner(SellModePropagandaAdapter.AdapterOnclickListner listner) {
                this.listner = listner;
        }

        public void setItemClickListener(OnItemClickListener itemClickListener) {
                mItemClickListener = itemClickListener;
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        //执行
                        case R.id.btn_1:

                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.onclick(R.id.btn_1);
                                        }
                                }
                                break;
                        case R.id.btn_name:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.onclick(R.id.btn_name);
                                        }
                                }
                                break;
                        case R.id.btn_delet:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.onclick(R.id.btn_delet);
                                        }
                                }
                                break;

                }

        }

}
